import 'package:flutter/material.dart';
import 'package:full_display/common/qxp_color.dart';
import 'package:full_display/common/qxp_style.dart';
import 'package:full_display/page/montage/montage_provider.dart';
import 'package:full_display/unit/screen/screen_utility.dart';
import 'package:provider/provider.dart';

class MSNCodeDialogWidget extends StatelessWidget {
  Function(String sn) sncode;
  MSNCodeDialogWidget({Key? key, required this.sncode}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [ChangeNotifierProvider(create: (_) => MontageProvider())],
      child: MSNCodeDialog(
        sncode: sncode,
      ),
    );
  }
}

class MSNCodeDialog extends StatefulWidget {
  Function(String sn) sncode;
  MSNCodeDialog({super.key, required this.sncode});

  @override
  State<MSNCodeDialog> createState() => _MSNCodeDialogState();
}

class _MSNCodeDialogState extends State<MSNCodeDialog> {
  var _montageProvider = MontageProvider();
  @override
  Widget build(BuildContext context) {
    _montageProvider = context.watch<MontageProvider>();
    return Container(
      width: 320.0.W,
      height: 380.0.W,
      decoration: const BoxDecoration(
        gradient: LinearGradient(
            //渐变位置
            begin: Alignment.topCenter, //右上
            end: Alignment.bottomCenter, //左下
            //渐变颜色[始点颜色, 结束颜色]
            colors: [ColorPlate.app_blue, ColorPlate.sub_blue]),
        borderRadius: BorderRadius.all(Radius.circular(8.0)),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Padding(
            padding: EdgeInsets.fromLTRB(10.0.W, 20.0.H, 10.0.W, 20.0.H),
            child: Column(
              children: [
                Text(
                  "选择SN码",
                  style: StandardTextStyle.textWhite17Style,
                  textAlign: TextAlign.center,
                ),
                Container(
                  height: 15.0.H,
                ),
                Container(
                  height: 300.0.W,
                  child: GridView.builder(
                    padding: EdgeInsets.zero,
                    scrollDirection: Axis.vertical,
                    physics: const AlwaysScrollableScrollPhysics(),
                    shrinkWrap: true,
                    itemCount: _montageProvider.SNCodeList.length,
                    itemBuilder: (con, index) {
                      return SNCodeItem(
                        _montageProvider.SNCodeList[index],
                        index,
                      );
                    },
                    gridDelegate:
                        const SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 1,
                      crossAxisSpacing: 1,
                      mainAxisSpacing: 10,
                      //宽高比
                      childAspectRatio: 28 / 5,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget SNCodeItem(String name, int index) {
    return InkWell(
      onTap: () {
        widget.sncode(name);
        Navigator.pop(context);
      },
      child: Container(
        padding: const EdgeInsets.all(10),
        width: 280.0.W,
        height: 50.0.W,
        decoration: BoxDecoration(
          //设置边框
          border: Border.all(color: Colors.white, width: 1),
          //背景颜色
          color: ColorPlate.backgroundOpacity0,
          //设置圆角
          borderRadius: BorderRadius.circular((25.0.W)),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(name,
                      style: StandardTextStyle.textWhite17Style,
                      textAlign: TextAlign.center,
                      overflow: TextOverflow.ellipsis),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
